<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" session="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0">
    <title>智能门禁安装</title>

    <link href="${pageContext.request.contextPath}/static/css/common.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/css/bootstrap.min.css"
          rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/font-awesome-4.3.0/css/font-awesome.min.css"
          rel="stylesheet">
    <!-- jqgrid-->
    <link href="${pageContext.request.contextPath}/static/plugins/jqGrid/css/custom.ui.jqgrid.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/layer/skin/layer.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/jqGrid/css/style.css" rel="stylesheet">

    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery.form.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/common.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/jqGrid/js/i18n/grid.locale-cn.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/jqGrid/js/minified/jquery.jqGrid.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/layer/layer.js" type="text/javascript"></script>

    <style type="text/css">
        .main h5 a {
            position: absolute;
            right: 20px;
            font-size: 14px;
            bottom: 0px;
            display: inline-block;
            padding: 5px;
        }

        .table_list tbody tr {
            height: 35px;
            text-align: center;
        }
        .jqGrid_wrapper{
        *+margin-left:270px;
        }
        .left ul li{
			
			*line-height:40px;
			
			*padding-left:20px;
		}
		
		.left ul li a, .left ul li a:hover{
			*color:#fff;
		}
    </style>
</head>
<body view="menu-item-4142">
<jsp:include page="../../top.jsp"></jsp:include>
<div class="right">
    <div class="wrapper">
	    <div class="jqGrid_wrapper">
	        <div id="jqGrid_operation" class="jg-grid">
	            <h1>智能门禁安装</h1>
	            <div class="jqGrid_operation" id="action"></div>
	        </div>
	        <table id="jqGrid"></table>
	        <div id="jqGrid_paper"></div>
	    </div>
	</div>
</div>
<script>
    $(document).ready(
            function () {
                $.jgrid.defaults.styleUI = "Bootstrap";
                var lastSel;
                $("#jqGrid").jqGrid(
                        {
                            url: "${pageContext.request.contextPath}/admin/tlocks_installTlocksList.action",
                            mtype: 'POST',
                            datatype: "json",
                            height: 350,
                            autowidth: true,
                            shrinkToFit: true,
                            rowNum: 10,
                            rowList: [10, 20, 30],
                            colNames: ['序号', 'PID编号', '类型', '楼栋', '单元', '门禁名称','操作'],
                            colModel: [{
                                name: "SID",
                                index: "SID",
                                key: true,
                                hidden: true
                            }, {
                                name: 'PID',
                                index: 'PID'
                            }, {
                                name: 'TYPE',
                                index: 'TYPE',
                                editable: true,
                                formatter:'select',
                                edittype:'select',
                                editoptions: {value:"0:单元门禁;1:公共门禁"},
                                formatter: function (cellvalue, options, rawObject) {
                                    if(typeof(cellvalue) != "undefined"){
                                    	if(cellvalue == '双击请点击选择门禁类型'){
                                    		return '双击请点击选择门禁类型';
                                       	}else if(cellvalue == '1'){
                                        	return '公共门禁';
                                        }else{
                                        	return '单元门禁';
                                        }
                                    }else{	
                                		return '双击请点击选择门禁类型';
                                	}
                                }
                            },{
                                name: 'BUILDING',
                                index: 'BUILDING',
                                editable: true,
                                formatter:'select',
                                edittype:'select',
                                editoptions:{value:getBudiling()}
//                                 formatter: function (cellvalue, options, rawObject) {
//                                 	if(!rawObject.SID){
//                                     	return ''
//                                     }else{	
//                                     	return '请点击选择楼栋';
//                                     }
//                             	}
                            },{
                                name: 'UNIT',
                                index: 'UNIT',
                                editable: true,
                                formatter:'select',
                                edittype:'select',
                                editoptions:{value:getUnit()}
//                                 formatter: function (cellvalue, options, rawObject) {
//                                 	if(!rawObject.SID){
//                                     	return ''
//                                     }else{	
//                                     	return '请点击选择单元';
//                                     }
//                             	}
                            },{
                                name: 'NAME',
                                index: 'NAME',
                                editable:true,
                                edittype:"text"
                            },{
                                name: 'BU',
                                index: 'BU'
                            }],
                            sortname: 'sid',
                            sortorder:'desc',
                            rownumbers: true,
                            jsonReader: {
                                root: 'rows',
                                page: "page",
                                total: 'total',
                                records: "records",
                                repeatitems: false
                            },
                            pager: "#jqGrid_paper",
                            viewrecords: true,
                            multiselect: true,
                            hidegrid: false,
                            //双击改变值
                            ondblClickRow: function(id){
                                //取当前行的值
                                var rowData = $("#jqGrid").jqGrid("getRowData", id); 
                            	if(!rowData.SID){
                            		return;
                            	}
                                if(id && id!==lastSel){ 
                                   jQuery('#jqGrid').restoreRow(lastSel); 
                                   var td = $("#"+lastSel).find("td:last");
                                   td.html("");
                                   lastSel=id;
                                }
                                //将当前行edit
                                jQuery('#jqGrid').editRow(id,true);
                               
                                //默认安装点名称
                                $("#"+id+"_UNIT").attr("disabled", "true");
                                setName(id);
                                //type变化
                                $("#"+id+"_TYPE").change(function() {  
                                	 var selectType = $("#"+id+"_TYPE").val();
                               		 $("#"+id+"_BUILDING").empty();  
                               	     $("#"+id+"_UNIT").empty(); 
                                	 //公共门禁
                               	     if(selectType == 1){
                                		 $("#"+id+"_BUILDING").append("<option>-</option>");  
                                	     $("#"+id+"_UNIT").append("<option>-</option>");
                                	     $("#"+id+"_BUILDING").attr("disabled", "true");
                                	     $("#"+id+"_UNIT").attr("disabled", "true");
                                	     $("#"+id+"_NAME").val("");
                                	     return;
                                	 }
                                	 //单元门禁
                                	 $("#"+id+"_BUILDING").removeAttr("disabled");
                            	     $("#"+id+"_UNIT").removeAttr("disabled");
                                	 $.ajax( {     
                       	                url:"${pageContext.request.contextPath}/admin/cbuilding_list.action",  
                       	                type:"post",  
                       	                dataType:"json", 
                       	                error:function(){alert("请求失败!");},  
                       	                success:function(data){  
                       	                    if(data!=null && data!=""){  
                       	                        var str="";  
                       	                        $("#"+id+"_BUILDING").append("<option value='-1'>请选择</option>");  
                       	                     	$("#"+id+"_UNIT").empty(); 
                        	                    $("#"+id+"_UNIT").append("<option value='-1'>请选择</option>");  
                       	                        for(var i=0;i<data.length;i++){  
                       	                        str+="<option value='"+data[i].sid+"'>"+data[i].cbName+"</option>";   
                       	                        }   
                       	                        $(str).appendTo($("#"+id+"_BUILDING"));    
                       	                        }  
                       	                    }  
                              	      });  
                                });
                                
                                $("#"+id+"_BUILDING").change(function() {  
	                               	 var selectBuilding = $("#"+id+"_BUILDING").val();
	                               	 $("#"+id+"_UNIT").empty(); 
	                               	 $("#"+id+"_UNIT").removeAttr("disabled");
                               	     $.ajax( {     
                      	                url:"${pageContext.request.contextPath}/admin/cinstallunit_listUnit.action",  
                      	                type:"post",  
                      	                data:{
                      	                	'bsid':selectBuilding,
                      	                	'type':'ins'
                      	                },
                      	                dataType:"json",  // 接受数据格式  
                      	                error:function(){alert("请求失败!");},  
                      	                success:function(data){  
                      	                   var str="";  
                      	                 	$("#"+id+"_UNIT").empty(); 
                      	                    $("#"+id+"_UNIT").append("<option value='-1'>请选择</option>");  
                      	                    if(data!=null && data!=""){  
                      	                        for(var i=0;i<data.length;i++){  
                      	                        str+="<option value='"+data[i].SID+"'>"+data[i].CBNAME+"</option>";   
                      	                        }   
                      	                        $(str).appendTo($("#"+id+"_UNIT"));    
                      	                        }  
                      	                    }  
                             	      });
                               	  setName(id);
                               });
                                
                                $("#"+id+"_UNIT").change(function() {  
                                	setName(id);
                              });
                                
                                var td = $("#"+id).find("td:last");
                                td.html("<input type=\"button\"  style=\"padding: 2px 10px\" class=\"btn btn-success\" value=\"保存\" onclick=\"save("+rowData.SID+","+id+")\" \/><input type=\"button\"  style=\"padding: 2px 10px\" class=\"btn btn-success\" value=\"取消\" onclick=\"cancle("+rowData.SID+","+id+")\" \/>");
                              }
                        });
			
            });

    
    <!--刷新jqGrid表格数据-->
    function refreshGrid() {
        $("#jqGrid").trigger("reloadGrid");
    }
    function cancle(sid,id){
    	jQuery('#jqGrid').restoreRow(id); 
    	var td = $("#"+id).find("td:last");
        td.html("");
    }
    function save(sid,id){
    	var type = $("#"+id+"_TYPE").val();
    	var name = $("#"+id+"_NAME").val();
    	if(type == 1){
    		if(!name || name ==""){
    			alert("公共门禁必须填门禁名称");
    			return;
    		}
    	}else{
	    	var building = $("#"+id+"_BUILDING").val();
	    	var unit = $("#"+id+"_UNIT").val();
    		if(!building  || building == -1 || !unit || unit == -1){
    			alert("单元门禁必须选择楼栋");
    			return;
    		}
    	}
//     	jQuery("#jqGrid").saveRow(id, false, 'clientArray');
    	jQuery("#jqGrid").saveRow(id, {  
             url: "${pageContext.request.contextPath}/admin/tlocks_installTlocks.action",  
             mtype : "POST",  
             restoreAfterError: true,  
             extraparam: { 
            	  "id": sid,
	              "type": $("#"+id+"_TYPE").val(),  
	              "name": $("#"+id+"_NAME").val(),  
	              "building": $("#"+id+"_BUILDING").val(),  
	              "unit": $("#"+id+"_UNIT").val()
             },  
             successfunc: function(response){  
            	 response = JSON.parse(response.responseText)
                 if (response.code != 1) {
                	 alert(response.msg);
                	 return false;
               	 }else{
               		location.reload();
//                  	window.location.href = "${pageContext.request.contextPath}/admin/tlocks_installTlocksView.action";
                 	return true; 
                 }
             }, 
             errorfunc: function(rowid, res){  
                 console.log(res);  
             }  
         });
    	var td = $("#"+id).find("td:last");
        td.html("");
   	 }
    function getBudiling() {  
        var str="-1:请选择";  
    	$.ajax( {     
               url:"${pageContext.request.contextPath}/admin/cbuilding_list.action",  
               type:"post", 
               async:false,
               dataType:"json",  // 接受数据格式  
               error:function(){alert("请求失败!");},  
               success:function(data){  
                   if(data!=null && data!=""){  
                       for(var i=0;i<data.length;i++){  
//                     	   if(str != ""){
//                     		   str+=";";
//                     	   }
                       	   str+=";"+data[i].sid+":"+data[i].cbName;   
                       } 
                       
                   } 
                }  
  	      });  
         return str;
    }  
    
    function getUnit() {  
        var str="-1:请选择"; 
        var first = "first";
    	$.ajax( {     
               url:"${pageContext.request.contextPath}/admin/cinstallunit_listUnit.action",  
               type:"post", 
               data:{
	                	'bsid':first
	                },
               async:false,
               dataType:"json",  // 接受数据格式  
               error:function(){alert("请求失败!");},  
               success:function(data){  
                   if(data!=null && data!=""){  
                       for(var i=0;i<data.length;i++){  
//                     	   if(str != ""){
//                     		   str+=";";
//                     	   }
                       	    str+=";"+data[i].sid+":"+data[i].cbName;   
                       } 
                   } 
                }  
  	      });  
         return str;
    }  
    
    function setName(id){
    	var buding = $("#"+id+"_BUILDING").find("option:selected").text();
        var unit = $("#"+id+"_UNIT").find("option:selected").text();
        if(unit=='请选择'){
        	$("#"+id+"_NAME").val(buding);
        }else{
        	$("#"+id+"_NAME").val(buding+unit);
        }
    }
</script>
</body>
</html>
